package com.whately.core.base.utils;

import com.fasterxml.jackson.databind.JavaType;
import com.google.common.collect.Lists;
import com.google.gson.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class GsonUtils {
    //不用创建对象,直接使用Gson.就可以调用方法
    private static final Gson gson = new Gson();

    //转换器
    private static JsonParser gsonJsonParser=new JsonParser();

    //无参的私有构造方法
    private GsonUtils() {
    }

    /**
     * 获得 GsonUtil 默认的内置Gson 对象
     * @return Gson
     */
    public static Gson getDefaultGson() {
        return gson;
    }

    /**
     * 将对象转成json格式
     *
     * @param object
     * @return String
     */
    public static String toJsonString(Object object) {
        String json = gson.toJson(object);
        return json;
    }

    public static JsonObject string2JsonObject(String strJson) {
        JsonObject jo = null;
        try{
            if(StringUtils.isNotBlank(strJson)){
                jo = gsonJsonParser.parse(strJson).getAsJsonObject();
            }
        }catch (Exception e){
            log.error("string2JsonObject error ",e);
        }
        return jo;
    }


    public static JsonArray string2JsonArray(String strJson) {
        JsonArray ja = null;
        try {
            if(StringUtils.isNotBlank(strJson)){
                ja = gsonJsonParser.parse(strJson).getAsJsonArray();
            }
        }catch (Exception e){
            log.error("string2JsonArray error ",e);
        }
        return ja;
    }

    /**
     * 将json转成特定的cls的对象
     *
     */
    public static <T> T jsonToBean(String gsonString, Class<T> cls) {
        //传入json对象和对象类型,将json转成对象
        T  t = gson.fromJson(gsonString, cls);
        return t;
    }


    /**
     * 转List<对象>
     */
    public static <T> List<T> jsonToList(String json, Class<T> cls) {
        List<T> list = new ArrayList<T>();
        JsonArray array = new JsonParser().parse(json).getAsJsonArray();
        for (final JsonElement elem : array) {
            list.add(gson.fromJson(elem, cls));
        }
        return list;
    }


    /**
     * json字符串转成map的
     *
     * @param json
     * @return
     */
    public static <T> Map<String, T> jsonToMaps(String json, Class<T> cls) {
        Map<String, T> map = null;
        try{
            JavaType javaType = ObjectMapperUtil.getJavaType(Map.class,String.class,cls);
            map  =ObjectMapperUtil.getObjectMapper().readValue(json,javaType);
        }catch (Exception e){
            log.error("GsonUtils json to maps error ",e);
            throw new RuntimeException("GsonUtils json to maps error ",e);
        }
        return map;
    }

    public static <T> Map<Long, T> jsonToMapsLong(String json, Class<T> cls) {
        Map<Long, T> map = null;
        try{
            JavaType javaType = ObjectMapperUtil.getJavaType(Map.class,Long.class,cls);
            map  =ObjectMapperUtil.getObjectMapper().readValue(json,javaType);
        }catch (Exception e){
            log.error("GsonUtils json to maps error ",e);
            throw new RuntimeException("GsonUtils json to maps error ",e);
        }
        return map;
    }


    /**
     * json字符串转成list中有map的
     *
     * @return
     */
    public static <T> List<Map<String, T>> jsonToListMaps(String json, Class<T> cls) {
        List<Map<String, T>> list = null;
        try{
            JsonArray array = new JsonParser().parse(json).getAsJsonArray();
            if(array.size()>0){
                list = Lists.newArrayList();
            }

            JavaType tmpJavaTypeMap = ObjectMapperUtil.getJavaType(Map.class,String.class,cls);
            for (final JsonElement elem : array) {
                list.add(ObjectMapperUtil.getObjectMapper().readValue(elem.toString(),tmpJavaTypeMap));
            }
            /*
            JavaType javaType = ObjectMapperUtil.getCollectionType(List.class,tmpJavaTypeMap.getRawClass());
            list  =ObjectMapperUtil.getObjectMapper().readValue(json,javaType);
*/
           // list = ObjectMapperUtil.getObjectMapper().readValue(json,new TypeReference<List<Map<String,T>>>(){});
        }catch (Exception e){
            log.error("GsonUtils json to maps error ",e);
            throw new RuntimeException("GsonUtils json to list maps error ",e);
        }
        return list;
    }






}
